package com.htgd.gardenexpo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConFood;
import com.htgd.gardenexpo.entity.GeConTraffic;
import com.htgd.gardenexpo.page.FoodQuery;
import com.htgd.gardenexpo.page.TrafficQuery;
import com.htgd.gardenexpo.service.GeBaseUserService;
import com.htgd.gardenexpo.service.GeConTrafficService;
import com.htgd.gardenexpo.utils.SecurityParameter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 美食表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-04-15
 */
@RestController
@RequestMapping("/api/traffic")
@Api(tags = "交通信息")
public class GeConTrafficController {

    @Autowired
    private GeConTrafficService geConTrafficService;

    @Autowired
    private GeBaseUserService geBaseUserService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult<JSONObject> add (@RequestBody GeConTraffic geConTraffic){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConTrafficService.save(geConTraffic);
            result.setCode(200);
            result.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult<JSONObject> edit (@RequestBody GeConTraffic geConTraffic){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            if(geConTraffic.getEditorId() != null){
                geConTraffic.setEditorName(geBaseUserService.queryById(geConTraffic.getEditorId()).getUsername());
            }
            geConTrafficService.updateById(geConTraffic);
            result.setCode(200);
            result.setMessage("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult<JSONObject> delete (int id){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConTrafficService.removeById(id);
            result.setCode(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConTraffic>> list(@RequestBody TrafficQuery trafficQuery) {
        CommonResult<List<GeConTraffic>> result = new CommonResult<>();
        try {
            Page<GeConTraffic> list=geConTrafficService.querylist(trafficQuery);
            result.setCode(200);
            result.setData(list.getRecords());
            result.setMessage(String.valueOf(list.getTotal()));
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/review")
    @ApiOperation("审核")
    public CommonResult<JSONObject> review( GeConTraffic geConTraffic) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try{
            if (geConTraffic.getPublisherId()!=null){
                geConTraffic.setPublisherName(geBaseUserService.queryById(geConTraffic.getPublisherId()).getUsername());
                geConTraffic.setReleasetime(new Date());
            }
            if (geConTraffic.getAuthUserId()!=null){
                geConTraffic.setAuthUser(geBaseUserService.queryById(geConTraffic.getAuthUserId()).getUsername());
                geConTraffic.setAuthtime(new Date());
            }
            geConTrafficService.updateById(geConTraffic);
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/details")
    @ApiOperation("详情")
    @SecurityParameter
    public CommonResult<Map<String,Object>> details(@RequestBody String s){
        CommonResult<Map<String, Object>> result = new CommonResult<>();
        try{
            Integer id= JSON.parseObject(s).getInteger("id");
            Integer sign= JSON.parseObject(s).getInteger("sign");
            //点击加1
            if(sign!=null && sign ==1){
                addHit(String.valueOf(id));
            }
            Map<String, Object> map = new HashMap<>();
            GeConTraffic geConTraffic = geConTrafficService.getById(id);
            map.put("news",geConTraffic);
            result.setData(map);
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping("/addHit")
    @ApiOperation("点击量新增")
    public CommonResult<JSONObject> addHit(@RequestParam String id){
        CommonResult<JSONObject> result = new CommonResult<>();
        geConTrafficService.addHit(id);
        result.setCode(200);
        result.setMessage("增加成功");
        return result;
    }
}
